package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Goodtype(ctx *gin.Context) {

	var goodtype []models.Goodtype
	data_source.Db.Find(&goodtype)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    goodtype,
		"msg":     "ok",
	})
	return
}

type GoodtypeBody struct {
	ID    int64  `json:"id" gorm:"id"`
	Name  string `json:"name" gorm:"name"`
	Image string `json:"image" gorm:"image"`
}

func Goodtypeadd(ctx *gin.Context) {
	request := GoodtypeBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	image := request.Image
	gt := models.Goodtype{
		Name:  name,
		Image: image,
	}
	data_source.Db.Create(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Goodtypeupdate(ctx *gin.Context) {
	request := GoodtypeBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	image := request.Image
	gt := models.Goodtype{
		Name:  name,
		Image: image,
	}
	data_source.Db.Where("id = ?", id).Updates(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Goodtypedel(ctx *gin.Context) {
	request := GoodtypeBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Goodtype{})
	data_source.Db.Model(&models.Goods{}).Where("id = ?", id).Update("status", 2)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Goodpps(ctx *gin.Context) {

	var goodtype []models.Goodpp
	data_source.Db.Find(&goodtype)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    goodtype,
		"msg":     "ok",
	})
	return
}

func Goodppadd(ctx *gin.Context) {
	request := GoodtypeBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	gt := models.Goodpp{
		Name: name,
	}
	data_source.Db.Create(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Goodppupdate(ctx *gin.Context) {
	request := GoodtypeBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	gt := models.Goodpp{
		Name: name,
	}
	data_source.Db.Where("id = ?", id).Updates(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Goodppdel(ctx *gin.Context) {
	request := GoodtypeBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Goodpp{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type GoodsRBody struct {
	Data       []models.Goods `json:"data"`
	PageNo     int            `json:"pageNo"`
	PageSize   int            `json:"pageSize"`
	TotalCount int64          `json:"totalCount"`
	TotalPage  int64          `json:"totalPage"`
}

type GoodsBody struct {
	Status     int    `json:"status"`
	Page       int    `json:"page"`
	Pagesize   int    `json:"pagesize"`
	Goodtypeid int    `json:"goodtypeid"`
	Goodppid   int    `json:"goodppid"`
	Name       string `json:"name"`
	Shangcheng int64  `json:"shangcheng" gorm:"shangcheng"`
}

func Goods(ctx *gin.Context) {
	request := GoodsBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	shangcheng := request.Shangcheng
	goodtypeid := request.Goodtypeid
	goodppid := request.Goodppid
	name := request.Name
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Goods
	res := data_source.Db
	if name != "" {
		res = res.Where("name like ?", "%"+name+"%")
	}
	if status > 0 {
		res = res.Where("status = ?", status)
	}
	if shangcheng > 0 {
		res = res.Where("shangcheng = ?", shangcheng)
	}
	if goodtypeid > 0 {
		res = res.Where("goodtypeid = ?", goodtypeid)
	}
	if goodppid > 0 {
		res = res.Where("goodppid = ?", goodppid)
	}
	res.Model(&models.Goods{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := GoodsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type GoodBody struct {
	ID         int64   `json:"id" gorm:"id"`
	Goodtypeid int64   `json:"goodtypeid" gorm:"goodtypeid"`
	Name       string  `json:"name" gorm:"name"`
	Image      string  `json:"image" gorm:"image"`
	Price      float64 `json:"price" gorm:"price"` // 售价
	//Mojing        float64 `json:"mojing" gorm:"mojing"`               // 魔晶购买价
	Markeprice float64 `json:"markeprice" gorm:"markeprice"` // 市场价
	//Recoveryprice float64 `json:"recoveryprice" gorm:"recoveryprice"` // 回收价
	Buyingprice float64 `json:"buyingprice" gorm:"buyingprice"` // 进货价
	Status      int64   `json:"status" gorm:"status"`
	Goodppid    int64   `json:"goodppid" gorm:"goodppid"`
	Shangcheng  int64   `json:"shangcheng" gorm:"shangcheng"`
}

func Goodadd(ctx *gin.Context) {
	request := GoodBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	image := request.Image
	goodtypeid := request.Goodtypeid
	price := request.Price
	goodppid := request.Goodppid

	markeprice := request.Markeprice
	mojing := price * 2 * 3.14

	buyingprice := request.Buyingprice
	recoveryprice := buyingprice * 2 * 3.14
	gt := models.Goods{
		Name:          name,
		Image:         image,
		Goodtypeid:    goodtypeid,
		Price:         price,
		Mojing:        mojing,
		Markeprice:    markeprice,
		Recoveryprice: recoveryprice,
		Buyingprice:   buyingprice,
		Status:        1,
		Goodppid:      goodppid,
		Shangcheng:    2,
	}
	data_source.Db.Create(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Goodupdate(ctx *gin.Context) {
	request := GoodBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	image := request.Image
	goodtypeid := request.Goodtypeid
	price := request.Price
	markeprice := request.Markeprice
	mojing := price * 2 * 3.14
	buyingprice := request.Buyingprice
	recoveryprice := buyingprice * 2 * 3.14
	goodppid := request.Goodppid
	gt := models.Goods{
		Name:          name,
		Image:         image,
		Goodtypeid:    goodtypeid,
		Price:         price,
		Mojing:        mojing,
		Markeprice:    markeprice,
		Recoveryprice: recoveryprice,
		Buyingprice:   buyingprice,
		Status:        1,
		Goodppid:      goodppid,
	}
	data_source.Db.Where("id = ?", id).Updates(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Goodgrounding(ctx *gin.Context) {
	request := GoodBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	status := request.Status
	data_source.Db.Model(&models.Goods{}).Where("id = ?", id).Update("status", status)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Goodshangcheng(ctx *gin.Context) {
	request := GoodBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	shangcheng := request.Shangcheng
	data_source.Db.Model(&models.Goods{}).Where("id = ?", id).Update("shangcheng", shangcheng)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Goodinfo(ctx *gin.Context) {
	request := GoodBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var good models.Goods
	data_source.Db.Where("id = ?", id).First(&good)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    good,
		"msg":     "ok",
	})
	return
}
